package com.wxy.fliter;


import com.alibaba.fastjson.JSON;
import com.wxy.common.BaseContext;
import com.wxy.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.AntPathMatcher;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

    //@Component
    @WebFilter(filterName = "loginCheckFilter",urlPatterns = "/*")
    @Slf4j
    public class LoginCheckFilter implements Filter{
        public int verify(HttpServletRequest request){
            //学生
            Long userid1 = (Long) request.getSession().getAttribute("student");
            if (userid1 != null)
                return 1;
            //老师
            Long userid2 = (Long) request.getSession().getAttribute("teacher");
            if (userid2 != null)
                return 2;
            //管理员
            String root  = (String) request.getSession().getAttribute("root");
            if (root != null)
                return 3;
            return -1;
        }
        //路径匹配器
        public static final AntPathMatcher PATH_MATCHER = new AntPathMatcher();

        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {


            HttpServletRequest request = (HttpServletRequest) servletRequest;
            HttpServletResponse response = (HttpServletResponse) servletResponse;
            //获取请求的url
            String requestURI = request.getRequestURI();
            log.info("本次拦截:{}",requestURI);

            String[] urls = new String[]{
                    "/user/**"

            };

            //判断路径匹配
            boolean check = check(urls, requestURI);

            //如果不需要处理
            if(check){
                filterChain.doFilter(request,response);
                return;
            }

            if(verify(request) == 1){
                Long username = (Long) request.getSession().getAttribute("student");
                BaseContext.setCurrentId(username);

                filterChain.doFilter(request,response);
                return;
            }else if(verify(request) == 2){
                Long username = (Long) request.getSession().getAttribute("teacher");
                BaseContext.setCurrentId(username);

                filterChain.doFilter(request,response);
                return;
            }else if (verify(request) == 3){
                Long username = (Long) request.getSession().getAttribute("root");
                BaseContext.setCurrentId(username);

                filterChain.doFilter(request,response);
                return;
            }
            //判断是否登录了
//            if(request.getSession().getAttribute("user")!= null ){
//
//                Long username = (Long) request.getSession().getAttribute("user");
//                BaseContext.setCurrentId(username);
//
//                filterChain.doFilter(request,response);
//                return;
//            }

            //如果未登录,通过输出流
            response.getWriter().write(JSON.toJSONString(R.error("未登录！")));
            return;
        }

        /**
         * 路径匹配方法
         * @param requestURI
         * @return
         */
        public boolean check(String[] urls,String requestURI){
            for(String url : urls){
                boolean match = PATH_MATCHER.match(url, requestURI);
                if (match){
                    return match;
                }
            }
            return false;
        }
    }

